package cn.yikaojiuguo.student.ui.base;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.yikaojiuguo.student.R;

/**
 * Created by cfan_yang on 2015/12/8
 * Activity公用的自定义ActionBarActivity
 */
public class CustomerActionBarActivity extends BaseActivity{

    //添加到根布局下。自定义ActionBar以及子类布局以此为根布局
    private LinearLayout mContentView;

    //返回箭头
    protected ImageView mIv_back;
    //取消
    protected TextView mTv_back;
    //标题
    private TextView mTv_title;
    //完成图标
    protected ImageView mIv_operation;
    //完成
    private TextView mTv_operation;
    //ActionBar底部分隔线
    private View mDivider;
    //自定义ActionBar的父布局
    private RelativeLayout mRl_root;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        initContentView();
        initActionBar();
        setCustomerTitle();
    }

    protected void setCustomerTitle() {
        mTv_title.setText(getTitle());
        setCustomerTitleVisibility(true);
    }

    protected void setCustomerTitle(String title) {
        mTv_title.setText(title);
        setCustomerTitleVisibility(true);
    }

    protected void setCustomerBackImage(int resId) {
        mIv_back.setImageResource(resId);
    }

    protected void setCustomerOperationText(String text) {
        mTv_operation.setText(text);
        setViewVisibility(mTv_operation, true);
    }

    protected void setCustomerOperationImageResourse(int resId) {
        setViewVisibility(mIv_operation,true);
        setViewVisibility(mTv_operation, false);
        mIv_operation.setImageResource(resId);
    }

    protected void setCustomerOperationTextColor(int color) {
        mTv_operation.setTextColor(color);
    }

    protected void setCustomerBackgroundColor(int color) {
        mRl_root.setBackgroundColor(color);
    }

    protected void setDividerVisibility(boolean visibility) {
        setViewVisibility(mDivider, visibility);
    }

    protected void setCustomerBackText(String text) {
        setViewVisibility(mTv_back,true);
        setViewVisibility(mIv_back,false);
        mTv_back.setText(text);
    }

    protected void setCustomerOperationOnClickListener(View.OnClickListener listener) {
        if(mTv_operation.getVisibility() == View.VISIBLE) {
            mTv_operation.setOnClickListener(listener);
        } else if(mIv_operation.getVisibility() == View.VISIBLE) {
            mIv_operation.setOnClickListener(listener);
        }
    }

    protected void setCustomerBackVisibility(boolean visibility) {
        setViewVisibility(mIv_back,visibility);
        setViewVisibility(mTv_back,visibility);
    }

    protected void setCustomerTitleVisibility(boolean visibility) {
        setViewVisibility(mTv_title,visibility);
    }

    protected void setCustomerBackOnclickListener(View.OnClickListener listener) {
        if(mIv_back.getVisibility() == View.VISIBLE) {
            mIv_back.setOnClickListener(listener);
        } else if (mTv_back.getVisibility() == View.VISIBLE) {
            mTv_back.setOnClickListener(listener);
        }
    }

    private void setViewVisibility(View view, boolean visibility) {
        if(visibility) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.GONE);
        }
    }

    private void initActionBar() {
        View.OnClickListener back = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        };
        mIv_back = (ImageView) findViewById(R.id.iv_back);
        mTv_back = (TextView) findViewById(R.id.tv_back);
        mTv_title = (TextView) findViewById(R.id.tv_title);
        mIv_operation = (ImageView) findViewById(R.id.iv_operation);
        mTv_operation = (TextView) findViewById(R.id.tv_operation);
        mDivider = findViewById(R.id.divider);
        mRl_root = (RelativeLayout) findViewById(R.id.rl_root);
        // finish页面
        mIv_back.setOnClickListener(back);
        mTv_back.setOnClickListener(back);
    }

    /**
     * 将自定义ActionBar添加到根布局
     */
    private void initContentView() {
        //获取根布局(LinearLayout)下的布局(FrameLayout)
        ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
        rootView.removeAllViews();
        mContentView = new LinearLayout(this);
        mContentView.setOrientation(LinearLayout.VERTICAL);
        rootView.addView(mContentView);
        View.inflate(this, R.layout.layout_my_actionbar,mContentView);
    }

    @Override
    public void setContentView(int layoutResID) {
        View.inflate(this, layoutResID, mContentView);
    }

    @Override
    public void setContentView(View view) {
        mContentView.addView(view);
    }
}
